package com.shopping.mall.core.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.VO.AdminVo;
import com.shopping.mall.auth.VO.UserVo;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.admin.entity.Admin;
import com.shopping.mall.core.module.admin.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "管理员")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @ApiOperation("管理员查询自己信息")
    @GetMapping("/adminInfo")
    public Result<Object> adminInfo(){
        return adminService.adminInfo();
    }

    @ApiOperation("超级管理员查询管理员信息")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @GetMapping("/adminInfoBySuper")
    public Result<Object> adminInfo(@RequestParam String id){
        return adminService.adminInfo(id);
    }

    @ApiOperation("修改个人信息")
    @PutMapping("/changeAdminInfo")
    public Result<Object> changeAdminInfo(@Validated(AdminVo.changeAdminInfo.class) @RequestBody AdminVo adminVo){
        return adminService.changeAdminInfo(adminVo);
    }

    @ApiOperation("超级管理员修改管理员信息")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @PutMapping("/changeAdminInfoBySuper")
    public Result<Object> changeAdminInfoBySuper(@Validated(AdminVo.changeAdminInfoBySuper.class) @RequestBody AdminVo adminVo){
        return adminService.changeAdminInfoBySuper(adminVo);
    }

    @ApiOperation("登录")
    @UnLogin
    @PostMapping("/login")
    public Result<Object> login(@Validated(AdminVo.login.class) @RequestBody AdminVo adminVo){
        return adminService.login(adminVo);
    }

    @ApiOperation("退出登录")
    @DeleteMapping("/logout")
    public Result<Object> logout(){
        return adminService.logout();
    }

    @ApiOperation("刷新token")
    @UnLogin
    @PostMapping("/refreshToken")
    public Result<Object> refreshToken(@RequestBody Map<String,String> refreshTokenMap){
        return adminService.refreshToken(refreshTokenMap.get("refreshToken"));
    }

    @ApiOperation("添加管理员")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @PostMapping("/add")
    public Result<Object> add(@Validated(AdminVo.add.class) @RequestBody AdminVo adminVo){
        return adminService.add(adminVo);
    }

    @ApiOperation("分页查询")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @GetMapping("/page")
    public Result<IPage<Admin>> getAdminPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Admin admin) {
        IPage<Admin> adminPage=adminService.getAdminPage(current,size,admin);
        return Result.OK(adminPage);
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Admin> getById(@RequestParam String id){
        Admin admin=adminService.getById(id);
        return Result.OK(admin);
    }

    @ApiOperation("查询所有")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @GetMapping
    public Result<List<Admin>> getAll(Admin admin){
        List<Admin> adminList=adminService.getAll(admin);
        return Result.OK(adminList);
    }

    @ApiOperation("通过id删除")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return adminService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return adminService.batchDelete(ids);
    }

    @ApiOperation("用户列表")
    @GetMapping("userList")
    public Result<Object> userList(@RequestParam Integer current,@RequestParam Integer size){
        return adminService.userList(current,size);
    }

    @ApiOperation("管理员修改用户信息")
    @PostMapping("changeUserInfoByAdmin")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    public Result<Object> changeUserInfoByAdmin(@Validated(UserVo.changeUserInfoByAdmin.class) @RequestBody UserVo userVo) {
        return adminService.changeUserInfoByAdmin(userVo);
    }

}
